﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GeoWeb.DataContext;

namespace GeoWeb.Demo.ButtonConfigInfo
{
    /// <summary>
    /// 按钮配置功能数据库操作类
    /// add by zhangyu
    /// 2013.01.23
    /// </summary>
    public class ButtonInfoDal : GeoWeb.Demo.DataContext.DbUtity
    {
        /// <summary>
        /// 获取所有的按钮信息
        /// </summary>
        /// <returns></returns>
        public List<GeoButtonInfo> GetButtonList()
        {
            return Db.From<GeoButtonInfo>().OrderBy(GeoButtonInfo._.ID.Desc).ToList<GeoButtonInfo>() as List<GeoButtonInfo>;
        }

        /// <summary>
        /// 根据关键词查询按钮信息
        /// </summary>
        /// <param name="keyWords"></param>
        /// <returns></returns>
        public List<GeoButtonInfo> GetButtonListBySearch(string keyWords)
        {
            return Db.From<GeoButtonInfo>().Where(GeoButtonInfo._.Name.Like("%" + keyWords + "%")).OrderBy(GeoButtonInfo._.ID.Desc).ToList<GeoButtonInfo>() as List<GeoButtonInfo>;
        }
       

        /// <summary>
        /// 根据按钮的GUID获取一条记录
        /// </summary>
        /// <param name="buttonID"></param>
        /// <returns></returns>
        public GeoButtonInfo GetButtonByBtnID(string buttonID)
        {
            return Db.From<GeoButtonInfo>().Where(GeoButtonInfo._.ButtonID == buttonID).ToSingle<GeoButtonInfo>() as GeoButtonInfo;
        }

        /// <summary>
        /// 根据按钮的序号ID获取一条记录
        /// </summary>
        /// <param name="buttonID"></param>
        /// <returns></returns>
        public GeoButtonInfo GetButtonByID(int buttonID)
        {
            return Db.From<GeoButtonInfo>().Where(GeoButtonInfo._.ID == buttonID).ToSingle<GeoButtonInfo>() as GeoButtonInfo;
        }
        /// <summary>
        /// 根据按钮的序号IDs获取记录集合
        /// </summary>
        /// <param name="buttonID"></param>
        /// <returns></returns>
        public List<GeoButtonInfo> GetButtonByIDs(object[] buttonIDs)
        {
            return Db.From<GeoButtonInfo>().Where(GeoButtonInfo._.ID.In(buttonIDs)).ToList<GeoButtonInfo>() as List<GeoButtonInfo>;
        }
        /// <summary>
        /// 获取目前最大的ID
        /// </summary>
        /// <returns></returns>
        public int GetMaxID()
        {
            return Db.From<GeoButtonInfo>().Select(GeoButtonInfo._.ID.Max()).ToScalar<int>();
        }

        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int AddButtonInfo(GeoButtonInfo entity)
        {
            return Db.Save<GeoButtonInfo>(entity);
        }

        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int UpdateButtonInfo(GeoButtonInfo entity)
        {
            entity.Attach();
            Field[] Field_Arry = { GeoButtonInfo._.Name, GeoButtonInfo._.Description, GeoButtonInfo._.ButtonHTML, GeoButtonInfo._.JavaScript };
            object[] value_Arry = { entity.Name, entity.Description, entity.ButtonHTML, entity.JavaScript };
            return Db.Update<GeoButtonInfo>(Field_Arry, value_Arry, GeoButtonInfo._.ButtonID == entity.ButtonID);
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="ButtonID"></param>
        /// <returns></returns>
        public int DeleteButtonInfo(string ButtonID)
        {
            return Db.Delete<GeoButtonInfo>(GeoButtonInfo._.ButtonID == ButtonID);
        }
       
    }
}